﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Raven.Client;
using Raven.Client.Document;
using Raven.Client.Indexes;
using Raven.Client.Linq;

namespace RavenDbStudy
{
    public partial class Form1 : Form
    {
        private DocumentStore _documentStore;

        public Form1()
        {
            InitializeComponent();

            _documentStore = new DocumentStore() { Url = "http://bin-x200-win8:8080" };
            _documentStore.Initialize();

        }

        private IDocumentSession GetSession()
        {
            return _documentStore.OpenSession();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id;
            using (var session = Program.DocumentStore.OpenSession())
            {
                var person = new People() { Age = 13, Name = "Benny", Sex = Sex.Male };
                session.Store(person);
                session.SaveChanges();
                System.Diagnostics.Debug.WriteLine(person.Id);
                id = person.Id;
            }

            using (var session = Program.DocumentStore.OpenSession())
            {
                var p = session.Load<People>(id);
                p.Name = "modified name";
                session.SaveChanges();
                System.Diagnostics.Debug.WriteLine(p.Name);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CreateOrder();
        }

        private void CreateOrder()
        {
            using (var session = GetSession())
            {
                var product = new Product() { Description = "some product", Name = "ProductA" };
                session.Store(product);

                var customer = new Customer() { Name = "bin" };
                session.Store(customer);

                var orderline = new OrderLine() { Count = 23, Price = 123, ProductId = product.Id };
                var order = new Order() { OrderLines = new[] { orderline }, OrderTime = DateTime.Now, CustomerId = customer.Id };
                session.Store(order);

                session.SaveChanges();
            }
        }

        private void AddClassAndPeople()
        {
            using (var session = Program.DocumentStore.OpenSession())
            {
                var tutor = new People() { Name = "TutorA" };
                session.Store(tutor);

                var person = new People() { Name = "AStudent", TutorId = tutor.Id };
                session.Store(person);

                session.SaveChanges();



            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (var session = Program.DocumentStore.OpenSession())
            {
                var peoples = session.Query<People>()
                                     .Customize(x => x.Include<People>(p => p.TutorId))
                                     .ToList();

                foreach (var people in peoples)
                {
                    if (people.TutorId != null)
                    {
                        var tutor = session.Load<People>(people.TutorId);
                    }

                }

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            _documentStore.DatabaseCommands.PutIndex(
                "Orders/OrdersByDay",
                new IndexDefinitionBuilder<Order, OrdersPerDay>
                    {
                        Map = orders => from o in orders
                                        select new OrdersPerDay() { Count = 1, Day = o.OrderTime.Date },
                        Reduce = results => from result in results
                                            group result by result.Day
                                                into g
                                                select new OrdersPerDay() { Count = g.Sum(x => x.Count), Day = g.Key }
                                                
                    }

                );
        }

        private void button5_Click(object sender, EventArgs e)
        {
            using (var session = GetSession())
            {
                var orderWithCustomer = (from order in session.Query<Order, CustomerOrdersTask>()
                                         where order.CustomerId == "cusromers/1"
                                         select order).As<CustomerOrdersTask>().ToList();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var task = new CustomerOrdersTask();
            task.Execute(_documentStore);
        }
    }
}
